package com.stylefeng.guns.rest.modular.wz.api.v3.Dto;

import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: wz_dev_app_shop
 * @description: 腾讯推送dto
 * @author: Chenchao
 * @create: 2018-11-22 16:47
 **/
public class TencentPushDto {
    private static final long serialVersionUID = 1L;
    static AtomicInteger counter = new AtomicInteger(0);

    //推送消息格式
    public static JSONObject init(String title, String desc, String userCode) {
        Map<String, Object> map = new HashMap<>();
        //随机数 7天内不重复
        String random = DateUtil.toString(new Date(), new SimpleDateFormat("yyyyMMdd"));
        Long MsgRandom = Long.parseLong(random + counter);
        if (counter.intValue() < 99) {
            counter.getAndIncrement();
        } else {
            counter.set(0);
        }
        map.put("MsgRandom", MsgRandom);
        if (userCode != null) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("userCode", userCode);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("AttrsAnd", map1);
            map.put("Condition", map2);
        }
        //默认推送内容
        JSONObject MsgContent = new JSONObject();
        MsgContent.put("Text", title);
        List<Map<String, Object>> msgBody = new ArrayList<>();
        Map<String, Object> MsgType = new HashMap<>();
        MsgType.put("MsgType", "TIMTextElem");
        MsgType.put("MsgContent", MsgContent);
        msgBody.add(MsgType);
        map.put("MsgBody", msgBody);
        Map<String, Object> OfflinePushInfo = new HashMap<>();
        OfflinePushInfo.put("PushFlag", 0);
        OfflinePushInfo.put("Title", title);
        OfflinePushInfo.put("Desc", desc);
        map.put("OfflinePushInfo", OfflinePushInfo);
        return JSONObject.fromObject(map);
    }

    //获取属性格式
    public static JSONObject initGetAttr(String userCode) {
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList();
        list.add(userCode);
        map.put("To_Account", list);
        return JSONObject.fromObject(map);
    }

    //获取属性格式
    public static JSONObject initSetAttr(String userCode) {
        List<Map<String, Object>> list = new ArrayList();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("To_Account",userCode);
        Map<String,Object> userCodeMap=new HashMap<>();
        userCodeMap.put("userCode",userCode);
        Map<String,Object> Attrs=new HashMap<>();
        map2.put("Attrs",userCodeMap);
        list.add(map2);
        map.put("UserAttrs",list);
        return JSONObject.fromObject(map);
    }

}